#!/bin/bash
# Connecting to sftp server and upload backup file
# Replace user@hostname.here with yours
# Replace "yourpassword" with your sftp password.
# This is not secure in any way, don't use it on a server with valuable/private information.
# Replace "server_backup/picraft_mcserver/" with your remote backup folder.
# Replace "/home/pi/backup/" if needed, for the local backup folder.


# Get time via founction
Rd() {
  date +"%Y-%m-%d %H:%M:%S"
}

# get date time
Rd=`date +%Y%m%d`


# Log event
echo "[$(Rd)] Sftp: Connecting sftp server." >> /home/pi/scripts/log.txt
# End Log event 


/usr/bin/expect <<EOF

spawn sftp user@hostname.here
expect "password:"
send "yourpassword\n"
expect "sftp>"
send "cd server_backup/picraft_mcserver/\n"
expect "sftp>"
send "put /home/pi/backup/picraft_$Rd.7z\n"
expect "sftp>"
send "exit\n"
interact

EOF


# Log event
echo "[$(Rd)] Sftp: File uploaded." >> /home/pi/scripts/log.txt
# End Log event